<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layuimini-2/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuimini-2/css/layuimini.css" media="all">


</head>
<body>
<!-- 模糊搜索,数据表格绑定-->
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">就餐人姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="dinName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">食物风格</label>
                            <div class="layui-input-inline">
                                <select name="foodStyle" lay-verify="">
                                    <option value="">-----</option>
                                    <option value="1">中餐</option>
                                    <option value="2">西餐</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">味道</label>
                            <div class="layui-input-inline">
                                <select name="taste" lay-verify="">
                                    <option value="">---</option>
                                    <option value="1">美味</option>
                                    <option value="2">一般</option>
                                    <option value="3">不好</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">

                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" class="layui-btn layui-btn-primary"  ><i class="layui-icon"></i> 全部搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deleteBatch"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../../layuimini-2/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    var $ ;
    var form;
    var table;
    var layer;
    layui.use(['form','table',"jquery"], function () {
         $ = layui.jquery;
         form = layui.form;
         table = layui.table;

        //渲染表格
        tableRender();

        // 监听模糊搜索操作
        form.on('submit(data-search-btn)', function (obj) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: obj.field
            }, 'data');
            return false;
        });

        /**
         * toolbar监听添加,批量删除事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/view/hall/dining/diningAdd.html',
                    end: function () {
                        table.reload("currentTableId")
                    }
                });

            } else if (obj.event === 'deleteBatch') {  // 监听批量删除操作
                var checkStatus = table.checkStatus("currentTableId");
                if (checkStatus.data.length !=0) {
                    var arr = [];
                    for (var i=0;i < checkStatus.data.length;i++) {
                        arr.push(checkStatus.data[i].id);
                    }
                    $.ajax({
                        url: "/dining/batch.do",
                        data:{ids:arr},
                        type:"get",
                        traditional:true,
                        success: function (ret) {
                            if (ret.code==0) {
                                layer.msg("删除成功!")
                                table.reload("currentTableId");
                            }
                        },
                        error:function () {
                            layer.msg("服务器忙!");
                        }
                    })
                } else {
                    layer.alert("请选择需要删除的数据",{icon:0,title:'提示'} )
                }
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            // console.log(obj)
        });

        /**
         * 数据编辑,回显以及删除
         */
        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/view/hall/dining/diningEdit.html',
                    success: function (layero, index) {//弹出层open方法
                        let body = layer.getChildFrame("body", index);
                        let data = obj.data;
                        // console.log(houseData.id)
                        body.find("input[name=id]").val(data.id)
                        body.find("input[name=dinName]").val(data.dinName)
                        body.find("select[name=foodStyle]").val(data.foodStyle)
                        body.find("input[name=price]").val(data.price)
                        body.find("select[name=taste]").val(data.taste)
                        let date = new Date(data.diningTime);
                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        if (month < 10) {
                            month = "0" + month;
                        }
                        let day = date.getDate();
                        if (day < 10) {
                            day = "0" + day;
                        }
                        let addTime1 = year + "-" + month + "-" + day;
                        body.find("input[name=diningTime]").val(addTime1)

                    },
                    end: function () {
                        table.reload("currentTableId")
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm("确认删除", {icon: 3, title: "提示"}, function (index) {
                    //layer.msg("删除成功")
                    //发送请求
                    $.get("/dining/delete.do", {id: obj.data.id}, function (ret) {
                        if (ret.code == 0) {
                            layer.msg("删除成功")
                        } else {
                            layer.msg("删除失败")
                        }
                    });
                    layer.close(index);
                    table.reload("currentTableId")
                }, function (index) {
                    layer.msg("取消删除")
                    layer.close(index)
                });
            }
        });
    });

    //表格渲染
    function tableRender(obj) {
        table.render({
            elem: '#currentTableId',
            url: '/dining/list.do',
            toolbar: '#toolbarDemo',
            page: true,
            limit: 6,
            limits: [6, 12, 18, 24],
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'dinName', width: 110, title: '就餐人姓名'},
                {field: 'foodStyle', title: '食物风格', width: 110,templet: function (obj) {
                        return obj.foodStyle == 1 ? '中餐' : '西餐';}
                },
                {field: 'price', title: '价格', width: 70},
                {field: 'taste', title: '食物味道', width: 110,templet: function (obj) {
                        if (obj.taste == 1) {
                            return '美味';
                        } else if (obj.taste == 2) {
                            return '一般';
                        } else if (obj.taste == 3) {
                            return '不好'
                        }
                    }
                },
                {field: 'diningTime', width: 120, title: '就餐时间', sort: true, templet: function (obj) {
                        let date = new Date(obj.diningTime);
                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        if (month < 10) {
                            month = "0" + month
                        }
                        let day = date.getDate();
                        if (day < 10) {
                            day = "0" + day
                        }
                        return year + "-" + month + "-" + day
                    }},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            skin: 'line'
        });
    }


</script>

</body>
</html>